package com.hjm.project.agentInfo.point.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import com.hjm.project.agentInfo.point.domain.TaxPoint;
import com.hjm.project.agentInfo.point.mapper.TaxPointMapper;
import com.hjm.project.agentInfo.point.service.ITaxPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务商税点信息Service业务层处理
 * 
 * @author LOVE
 * @date 2020-12-03
 */
@Service
public class TaxPointServiceImpl implements ITaxPointService
{
    @Autowired
    private TaxPointMapper taxPointMapper;
    @Autowired
    private AgentInfoMapper agentInfoMapper;

    /**
     * 查询服务商税点信息
     * 
     * @param tpNum 服务商税点信息ID
     * @return 服务商税点信息
     */
    @Override
    public TaxPoint selectTaxPointById(Long tpNum)
    {
        return taxPointMapper.selectTaxPointById(tpNum);
    }

    /**
     * 查询服务商税点信息列表
     * 
     * @param taxPoint 服务商税点信息
     * @return 服务商税点信息
     */
    @Override
    public List<TaxPoint> selectTaxPointList(TaxPoint taxPoint)
    {
        return taxPointMapper.selectTaxPointList(taxPoint);
    }

    /**
     * 新增服务商税点信息
     * 
     * @param taxPoint 服务商税点信息
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Map<String, String> insertTaxPoint(TaxPoint taxPoint)
    {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");
        TaxPoint t=new TaxPoint();
        t.setAddMan(ShiroUtils.getLoginName());
        t.setAgentNum(taxPoint.getAgentNum());
        t.setTaxPoint(taxPoint.getTaxPoint());
        t.setType(Long.valueOf("0"));
        taxPointMapper.insertTaxPoint(t);
        t.setTaxPoint(taxPoint.getTaxPointT());
        t.setType(Long.valueOf("4"));
        taxPointMapper.insertTaxPoint(t);
        t.setTaxPoint(taxPoint.getDQtaxPoint());
        t.setType(Long.valueOf("2"));
        taxPointMapper.insertTaxPoint(t);
        t.setTaxPoint(taxPoint.getDQtaxPointT());
        t.setType(Long.valueOf("3"));
        taxPointMapper.insertTaxPoint(t);

        return result;
    }

    /**
     * 修改服务商税点信息
     * 
     * @param taxPoint 服务商税点信息
     * @return 结果
     */
    @Override
    public Map<String, String>  updateTaxPoint(TaxPoint taxPoint)
    {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");
        List<Map<String, Object>> listSon = taxPointMapper.getAgentInfo(taxPoint.getAgentNum());
        for (Map map:listSon){
            TaxPoint t=new TaxPoint();
            int i=taxPointMapper.deletePointByAgent(map.get("AGENT_NUM").toString());
            if (i>0){
                t.setAddMan(ShiroUtils.getLoginName());
                t.setAgentNum(map.get("AGENT_NUM").toString());
                t.setTaxPoint(taxPoint.getTaxPoint());
                t.setType(Long.valueOf("0"));
                taxPointMapper.insertTaxPoint(t);
                t.setTaxPoint(taxPoint.getTaxPointT());
                t.setType(Long.valueOf("4"));
                taxPointMapper.insertTaxPoint(t);
                t.setTaxPoint(taxPoint.getDQtaxPoint());
                t.setType(Long.valueOf("2"));
                taxPointMapper.insertTaxPoint(t);
                t.setTaxPoint(taxPoint.getDQtaxPointT());
                t.setType(Long.valueOf("3"));
                taxPointMapper.insertTaxPoint(t);
            }
        }
        //获取下级服务商税点
//        BigDecimal key1=new BigDecimal(taxPoint.getTaxPoint());
//        BigDecimal key2=new BigDecimal(taxPoint.getTaxPointT());
//        List<Map<String, Object>> listSon = taxPointMapper.getSonTaxPoint(taxPoint.getAgentNum());
//        for(Map map : listSon){
//            //验证下级是否设置该类型税点
//            if(!Utils.IsNull(map)){
//                if (taxPoint.getType()==0){
//                    if (Integer.parseInt(String.valueOf(map.get("TYPE"))) ==0){
//                        if(key1.compareTo(new BigDecimal(map.get("TAX_POINT").toString())) == 1){
//                            result.put("code","01");
//                            result.put("msg","当前服务商税点不能小于下级服务商！");
//                            return result;
//                        }
//                    }else if (Integer.parseInt(String.valueOf(map.get("TYPE"))) ==4) {
//                        if (key2.compareTo(new BigDecimal(map.get("TAX_POINT").toString())) == 1) {
//                            result.put("code", "01");
//                            result.put("msg", "当前服务商税点不能小于下级服务商！");
//                            return result;
//                        }
//                    }
//                }else if (taxPoint.getType()==1){
//                    if (Integer.parseInt(String.valueOf(map.get("TYPE"))) ==2) {
//                        if (key1.compareTo(new BigDecimal(map.get("TAX_POINT").toString())) == 1) {
//                            result.put("code", "01");
//                            result.put("msg", "当前服务商税点不能小于下级服务商！");
//                            return result;
//                        }
//                    }else if (Integer.parseInt(String.valueOf(map.get("TYPE"))) ==3){
//                        if(key2.compareTo(new BigDecimal(map.get("TAX_POINT").toString())) == 1){
//                            result.put("code","01");
//                            result.put("msg","当前服务商税点不能小于下级服务商！");
//                            return result;
//                        }
//                    }
//                }else{
//                    result.put("code","01");
//                    result.put("msg","配置失败！");
//                    return result;
//                }
//
//
//            }
//        }
        return result;
    }

    /**
     * 删除服务商税点信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTaxPointByIds(String ids)
    {
        return taxPointMapper.deleteTaxPointByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务商税点信息信息
     * 
     * @param tpNum 服务商税点信息ID
     * @return 结果
     */
    @Override
    public int deleteTaxPointById(Long tpNum)
    {
        return taxPointMapper.deleteTaxPointById(tpNum);
    }
}
